var MediovskiWeb = {
	initPage: function(){
		this.settings();
		this.loading(true);
		this.initNavi();
		this.initTopCarousel();
		this.initTabs('.about-us-text-bottom li', '.about-us-list li'); 
		this.initCaseLink();
		this.initAboutCarousel();
		this.initCarousel();
		this.initAjaxLink('#what-we-do-wrap .t1 span a');
		this.initPopup();
		this.initExternalLink();
		this.initEmailLink();
		this.initParallax();
	},
	initNavi: function(){
		var menuLinks = $('#menu a');
		
		menuLinks.bind('click', function(){
			var self = this;
			$.scrollTo( self.hash, 1000,{ 
				onAfter:function(){ location.hash = self.hash}
			});
			
			return false;
		});
		
		$('.logo a').click(function(){
			$.scrollTo( $('body'), 1000);
			location.hash = this.hash
		});

		$(document).bind('scroll', function(){
			var pagesCords = [];
			for(var i = 0; i < menuLinks.length; i++){
				var pagePosition = $(menuLinks.eq(i).attr('href')+'-wrap').position();
				pagesCords.push(parseInt(pagePosition.top, 10))
			}
			
			var navi = $('#header');
			var position = navi.position();
			
			for(var i = 0; i < pagesCords.length; i++){
				if(position.top >= pagesCords[i] && position.top < pagesCords[i+1]){
					menuLinks.removeClass('active');
					menuLinks.eq(i).addClass('active');
				}
			}

			if(position.top < pagesCords[0]){
				menuLinks.removeClass('active');
			}else if(position.top >= pagesCords[5]){
				menuLinks.removeClass('active');
				menuLinks.eq(5).addClass('active');
			}
		});
	},
	initTopCarousel: function(){
		var carouselWrapper = $("#carousel-wrapper");
		var controls = $('#carousel-wrapper .jcarousel-control a');
		var navi = $('#navi-carousel');
		var carouselText = $('#carousel-wrapper .visual-text');
		var slideLink = $('.slide-link');
		var nextSlide = $('.read-more-about-us .next');
		
		slideLink.bind('click', function(){
			var speed = 400;
			var self = this;
			$.scrollTo( $(self).attr('href'), speed,{ 
				onAfter:function(){ location = self.hash}		
			});
			var t = setTimeout(function(){
				nextSlide.trigger('click');
			}, speed);
			
			return false;
		});
		
		controls.css({
			width: navi.width() / controls.length
		});
		
		carouselText.css({
			opacity:0.1
		});
		
		var mycarouselTopInitCallback = function(carousel) {
		    controls.bind('click', function() {
		    	var index = $(this).attr("id").split("_");
				carousel.scroll($.jcarousel.intval(index[1]));
		        return false;
		    });
		};
		
		var itemVisibleIn = function(carousel, li, index, state){
    		controls.removeClass('active');
	    	controls.eq(index - 1).addClass('active');

	    	$(li).find(carouselText).animate({
	    		opacity:1
	    	}, 1000);
	    	
	    	carouselText.mouseenter(function() {
				carousel.stopAuto();
			}).mouseleave(function(){
				carousel.startAuto();
			});
	    	
	    	if($(li).hasClass('white')){
	    		navi.removeClass('black').addClass('white');
	    	}else{
	    		navi.removeClass('white').addClass('black');
	    	}
    	}
    	
    	var itemVisibleOut = function(carousel, li, index, state){
	    	$(li).find(carouselText).animate({
	    		opacity:0.1
	    	}, 300);
    	}
		 
	    carouselWrapper.jcarousel({
	        scroll: 1,
	        vertical: true,
	        start: 1,
	        wrap: "last",
	        auto: 6,
	        easing: 'easeInBack',
	        animation: 300,
	        itemFallbackDimension: 619,
	        initCallback: mycarouselTopInitCallback,
	        itemVisibleInCallback: itemVisibleIn,
	        itemVisibleOutCallback: itemVisibleOut,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });

	},
	initTabs: function(tabsContainer, tabsNavi){
		var btnClose = $('.close-x');
		var aboutUs = $('#about-us-wrap');
		
		$(tabsNavi).find('a').bind('click', function (e) {
			$(tabsContainer).hide().filter(this.hash).fadeIn();
			$(tabsNavi).removeClass('active');
			$(this).parent().addClass('active');
			$.scrollTo( aboutUs, 500,{ 
				offset:-68		
			});
			e.preventDefault();
		});
		
		btnClose.bind('click', function(){
			$(tabsNavi).last().find('a').trigger('click');
		});
		
	},
	initCaseCarousel: function(){
		var carouselWrapper = $(".case-carousel-wrapper");
		var controls = $('.case-carousel-wrapper .jcarousel-control a');
		var navi = $('.case-navi-carousel');
		var carouselText = $('.case-carousel-wrapper .visual-text');
		var btnPrev = $('.case-navi .prev');
		var btnNext = $('.case-navi .next');
		
		var caseLink = $('.case-link');
		var caseCarouselContent = $('#case-carousel-content');
		var btnClose = $('.ico.close');
		var ie = $.browser.msie && $.browser.version < 9;
		
		caseCarouselContent.slideDown();
		$.scrollTo( caseCarouselContent, 300,{ 
			offset:-68			
		});

		
		btnClose.bind('click', function(){
			caseCarouselContent.slideUp();
			return false;
		});
		
		controls.css({
			width: navi.width() / controls.length
		});

		carouselText.css({
			opacity:0.1,
			background: ie ? '#fff' : 'transparent' // fix for png img
		});
		
		var mycarouselTopInitCallback = function(carousel) {
		    controls.bind('click', function() {
		    	var index = $(this).attr("id").split("_");
				carousel.scroll($.jcarousel.intval(index[1]));
		        return false;
		    });
		    
		    btnNext.bind('click', function() {
		        carousel.next();
		        return false;
		    });
		
		    btnPrev.bind('click', function() {
		        carousel.prev();
		        return false;
		    });
		};
		
		var itemVisibleIn = function(carousel, li, index, state){
    		controls.removeClass('active');
	    	controls.eq(index - 1).addClass('active');

	    	$(li).find(carouselText).animate({
	    		opacity:1
	    	}, 1000);
	    	
	    	carouselText.mouseenter(function() {
				carousel.stopAuto();
			}).mouseleave(function(){
				carousel.startAuto();
			});
	    	
	    	if($(li).hasClass('white')){
	    		navi.removeClass('black').addClass('white');
	    	}else{
	    		navi.removeClass('white').addClass('black');
	    	}
    	}
    	
    	var itemVisibleOut = function(carousel, li, index, state){
	    	$(li).find(carouselText).animate({
	    		opacity:0.1
	    	}, 300);
    	}
		 
	    carouselWrapper.jcarousel({
	        scroll: 1,
	        vertical: true,
	        start: 1,
	        easing: 'easeInBack',
	        animation: 300,
	        itemFallbackDimension: 619,
	        initCallback: mycarouselTopInitCallback,
	        itemVisibleInCallback: itemVisibleIn,
	        itemVisibleOutCallback: itemVisibleOut,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });

	},
	initCaseLink: function(){
		var caseLink =  $('.case-link-ajax');
		var caseCarouselContent = $('#case-carousel-content');
		var self = this;

		caseLink.bind('click', function(){
			
			var url = $(this).attr('href').split('#');
			var ajaxLink = url[1];
			
			self.loading(true);
			$.ajax({
				url: "ajax/" + ajaxLink + ".html",
				cache: false,
				context: document.body,
				success: function(data){
					caseCarouselContent.hide();
					caseCarouselContent.html(data);
					caseCarouselContent.slideUp();
					self.loading(false);
					self.initCaseCarousel();
				}, 
				error: function(){
					caseCarouselContent.prepend('Ups... can not loading content')
				}
			});
			return false;
		});
	},
	initAboutCarousel: function(){
		var carouselElement = $('#about-us-carousel');
		var btnNext = $('#about-us-wrap .slide-next');
		var btnPrev = $('.read-more-about-us .prev');
		
		var mycarouselInitCallback = function(carousel){
		    btnNext.bind('click', function() {
		        carousel.next();
		        return false;
		    });
		
		    btnPrev.bind('click', function() {
		        carousel.prev();
		        return false;
		    });	
	    }

		carouselElement.jcarousel({
	        scroll: 1,
	        itemFallbackDimension: 950,
	        initCallback: mycarouselInitCallback,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });
	},
	initCarousel: function(){
		var carouselElement = $('.carousel-color');
		var btnNext = $('.carousel-navi .next');
		var btnPrev = $('.carousel-navi .prev');
		var controls = $('.pagination a');
		
		var itemVisibleIn = function(carousel, li, index, state){
    		controls.removeClass('active');
	    	controls.eq((index-1)/3).addClass('active');
    	}
		
		var mycarouselInitCallback = function(carousel){
			controls.bind('click', function() {
				
		    	var index = $(this).attr("id").split("_");
				carousel.scroll($.jcarousel.intval(index[1]));
		        return false;
		    });
		    
		    btnNext.bind('click', function() {
		        carousel.next();
		        return false;
		    });
		
		    btnPrev.bind('click', function() {
		        carousel.prev();
		        return false;
		    });	
	    }

		carouselElement.jcarousel({
	        scroll: 3,
	        itemFallbackDimension: 275,
	        itemVisibleInCallback: itemVisibleIn,
	        initCallback: mycarouselInitCallback,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });
	},
	initAjaxLink: function(el){
		var ajaxContent = $('#what-we-do-wrap .inner-pad');
		var menuLink = $('#what-we-do-wrap .t1 span a');
		var startLink = $('.offer-page-link');
		var backToOffer = $('.back-to-offer');
		var startText = $('#what-we-do-start-text');
		var self = this;
		
		backToOffer.hide();
		
		$('#what-we-do-wrap .col').each(function(i){
			$(this).bind('click', function(){
				$(el).eq(i).trigger('click');
			});
		});
		
		startLink.bind('click', function(){
			backToOffer.hide();
			$.scrollTo( $('#what-we-do'), 300,{ 
				// offset:-68			
			});
			ajaxContent.hide();
			startText.fadeIn(1000);
			$(menuLink).removeClass('active');
			$(this).addClass('active');
			location.hash = this.hash
			return false;
		});
		
		var ajaxLink = [
			'interactive',
			'enterprise',
			'startup'
		];
		
		
		$(el).each(function(i){
			if(location.hash == '#'+ajaxLink[i]){
				$.scrollTo( $('#what-we-do'), 300,{ 
					// offset:-68			
				});
				backToOffer.show();
				
				self.loading(true);
		    	$('#menu-what-we-do').show();
		    	startText.fadeOut();
		    	$(menuLink).removeClass('active');
		    	startLink.removeClass('active');
		    	$(menuLink).eq(i).addClass('active');
				$.ajax({
					url: "ajax/" + ajaxLink[i] + ".html",
					cache: true,
					context: document.body,
					success: function(data){
						self.loading(false);
						ajaxContent.fadeOut(100);
						ajaxContent.html(data);
						ajaxContent.fadeIn();
						self.initCarousel();
					}, 
					error: function(){
						ajaxContent.prepend('Ups... can not loading content')
					}
				});
			}
		});

		$(el).each(function(i){
		    $(this).bind('click', function(e){
		    	backToOffer.show();
		    	var url = $(this).attr('href').split('#');
				var link = url[1];
				
	    		$.scrollTo( $('#what-we-do'), 300,{ 
					// offset:-68			
				});
		    	
		    	self.loading(true);
		    	$('#menu-what-we-do').show();
		    	startText.fadeOut();
		    	$(menuLink).removeClass('active');
		    	startLink.removeClass('active');
		    	$(menuLink).eq(i).addClass('active');
		    	$.ajax({
					url: "ajax/" + link + ".html",
					cache: true,
					context: document.body,
					success: function(data){
						self.loading(false);
						ajaxContent.fadeOut(100);
						ajaxContent.html(data);
						ajaxContent.fadeIn();
						self.initCarousel();
					}, 
					error: function(){
						ajaxContent.prepend('Ups... can not loading content')
					}
				});
				location.hash = this.hash
				e.preventDefault();
		    });
  		});
	},
	initPopup: function(){
		$(".popup-link").fancybox({
			'width'				: '75%',
			'height'			: '75%',
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
		$(".popup").fancybox({
			'padding'			: '0',
			'margin'			: '20',
			'autoScale'			: false,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'type'				: 'inline'
		});
	},
	initExternalLink: function(){
		var anchors = $('a');
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
				anchor.target = "_blank";
		}
	},
	settings: function(){
		this.loaderImage = './imgs/loader.png';
		this.loader = $('<img class="ad-loader" src="'+ this.loaderImage +'" />');
	},
	loading: function(bool, place) {
		var self = this;
		if(bool) {
			var loaderPosition = place || 'body';
			$(loaderPosition).prepend(self.loader);
			self.loader.show();
		} else {
			var delay = setTimeout(function() {
                self.loader.remove();
            }, 500);
			
		};
	},
	initEmailLink: function(){
		var a1 = 'mail';
		var a2 = 'to:';
		var a3 = 'mediovski';
		var a4 = '.pl';
		var a5 = 'info' + '@' + a3 + a4;
		var a6 = 'zapytania-ofertowe' + '@' + a3 + a4;
		var a7 = 'enquiry' + '@' + a3 + a4;
		var a8 = 'praca' + '@' + a3 + a4;
		var a9 = 'career' + '@' + a3 + a4;
		
		var target = $('.mail-to');
		var target2 = $('.mail-to-offer');
		var target3 = $('.mail-to-offer-com');
		var target4 = $('.mail-to-job');
		var target5 = $('.mail-to-job-com');
		target.html('<a href="' + a1 + a2 + a5 + '">' + a5 + '</a>' );
		target2.html('<a href="' + a1 + a2 + a6 + '">' + a6 + '</a>' );
		target3.html('<a href="' + a1 + a2 + a7 + '">' + a7 + '</a>' );
		target4.html('<a href="' + a1 + a2 + a8 + '">' + a8 + '</a>' );
		target5.html('<a href="' + a1 + a2 + a9 + '">' + a9 + '</a>' );
	},
	initParallax: function(){
		$('#visual-2').parallax({
	      'elements': [
	        {
	          'selector': '.aegon-eye',
	          'properties': {
	            'x': {
	              'background-position-x': {
	                'initial': 70,
	                'multiplier': 0.05,
	                'unit': '%',
	                'invert': false
	              }
	            },
	            'y': {
	              'background-position-y': {
	                'initial': 50,
	                'multiplier': 0.12,
	                'unit': '%',
	                'invert': false
	              }
	            }
	          }
	        }
	      ]
	    });
	}
};

$(function(){
	MediovskiWeb.initPage();
	MediovskiWeb.loading(false);
});

